#include <windows.h>
#include <string>
#include <vector>
#include <map>

// 使用宽字符解决中文编码问题
class CollaborationMessage {
public:
    std::wstring type;
    std::wstring sender;
    std::wstring content;
    
    CollaborationMessage(const std::wstring& t, const std::wstring& s, const std::wstring& c) 
        : type(t), sender(s), content(c) {}
    
    std::wstring Serialize() const {
        return type + L"|" + sender + L"|" + content;
    }
};

// 使用宽字符的AICoder插件
class AICoderPlugin {
private:
    std::vector<CollaborationMessage> messageHistory;
    bool initialized = false;

public:
    static AICoderPlugin* GetInstance() {
        static AICoderPlugin instance;
        return &instance;
    }
    
    bool Initialize() {
        if (initialized) return true;
        
        // 使用宽字符中文
        messageHistory.push_back(CollaborationMessage(L"SYSTEM", L"AICoder", L"插件初始化成功"));
        initialized = true;
        return true;
    }
    
    const wchar_t* GenerateCode(const wchar_t* requirement) {
        static std::wstring result;
        result = L"// AICoder 生成的代码\n";
        result += L"// 需求: " + std::wstring(requirement) + L"\n\n";
        result += L"#include <iostream>\n\n";
        result += L"void ai_generated_function() {\n";
        result += L"    std::cout << \"Hello from AICoder!\\\\n\";\n";
        result += L"    // 更多AI生成的代码...\n";
        result += L"}\n";
        
        // 记录消息
        messageHistory.push_back(CollaborationMessage(L"CODE_GEN", L"用户", requirement));
        
        return result.c_str();
    }
    
    const wchar_t* GetCollaborationHistory() {
        static std::wstring history;
        history = L"协作历史:\n";
        for (const auto& msg : messageHistory) {
            history += L"[" + msg.type + L"] " + msg.sender + L": " + msg.content + L"\n";
        }
        return history.c_str();
    }
    
    const wchar_t* GetPluginInfo() {
        return L"AICoder 插件 v1.0 - AI代码生成和协作工具";
    }
};

// DLL入口点
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved) {
    switch (reason) {
        case DLL_PROCESS_ATTACH:
            AICoderPlugin::GetInstance()->Initialize();
            break;
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}

// 宽字符版本导出函数
extern "C" __declspec(dllexport) const wchar_t* AICoder_GenerateCode(const wchar_t* requirement) {
    return AICoderPlugin::GetInstance()->GenerateCode(requirement);
}

extern "C" __declspec(dllexport) const wchar_t* AICoder_GetCollaborationHistory() {
    return AICoderPlugin::GetInstance()->GetCollaborationHistory();
}

extern "C" __declspec(dllexport) const wchar_t* AICoder_GetPluginInfo() {
    return AICoderPlugin::GetInstance()->GetPluginInfo();
}

extern "C" __declspec(dllexport) bool AICoder_Initialize() {
    return AICoderPlugin::GetInstance()->Initialize();
}

// ANSI版本兼容函数
extern "C" __declspec(dllexport) const char* AICoder_GenerateCodeA(const char* requirement) {
    static std::string result;
    // 简化处理，实际应该进行字符编码转换
    result = "// AICoder generated code\n// Requirement: ";
    result += requirement;
    result += "\n\n#include <iostream>\n\nvoid ai_generated_function() {\n    std::cout << \"Hello from AICoder!\\\\n\";\n}\n";
    return result.c_str();
}

// CodeBlocks插件标准接口
extern "C" __declspec(dllexport) const char* GetPluginName() {
    return "AICoder";
}

extern "C" __declspec(dllexport) const char* GetPluginVersion() {
    return "1.0.0";
}

extern "C" __declspec(dllexport) const char* GetPluginDescription() {
    return "AI Code Generation and Collaboration Plugin";
}
